const http = require("http");
const https = require("https");
const url = require("url");

http
  .createServer((req, res) => {
    const urlObj = url.parse(req.url, true);

    res.writeHeader(200, {
      "Content-Type": "application/json;charset=utf-8",
      //cors头
      "access-control-allow-origin": "*",
    });
    switch (urlObj.pathname) {
      case "/api/aaa":
        //客户端身份
        //这边不要直接传res参数，耦合性太高，可以用回调函数异步解耦
        httpGet((data) => {
          res.end(data);
        });
        break;
      default:
        res.end("404");
    }
  })
  .listen(5000, () => {
    console.log("server start success");
  });

function httpGet(cb) {
  let data = "";
  https.get(`https://www.xiaomiyoupin.com/api/auth/login/isloggedin`, (res) => {
    res.on("data", (chunk) => {
      data += chunk;
    });
    res.on("end", () => {
      console.log("111", data);
      cb(data);
    });
  });
}
